<template>

  <div class='container'>
    <!-- 发表文章的 Dialog 对话框 -->
    <el-dialog title="新增技巧" :visible="showDialog" :before-close="handleClose" width="70%">
      <!-- 表单 -->
      <el-form ref="addDeptRef" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="文章标题" prop="title">
          <el-input style="width:70%" v-model="formData.title" placeholder="请输入文章标签" />
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor v-model="formData.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input style="width:70%" v-model="formData.videoURL" placeholder="请输入视频地址" />
        </el-form-item>
      </el-form>
      <!-- footer插槽 -->
      <template v-slot:footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="pubArticle">发布</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/articles.js'
export default {
  name: 'Articles-Add',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 表单的数据
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: ['blur', 'change'] }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: ['blur', 'change'] }],
        videoURL: [{ required: true, message: '请输入视频地址', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 发布新增技巧
    pubArticle () {
      this.$refs.addDeptRef.validate(async valid => {
        if (!valid) return
        try {
          const ret = await add(this.formData)
          if (ret.status === 200) {
            // 添加成功,关闭弹窗
            this.$emit('update:showDialog', false)
            // 刷新列表
            // this.$parent.loadList()
            this.$emit('show')
            // 弹窗提示添加成功
            this.$message.success('添加成功')
            // 弹窗关闭后清空表单
            this.$refs.addDeptRef.resetFields()
          }
        } catch (err) {
          // 弹窗提示添加失败
          console.dir(err)
          this.$message.error('添加失败')
        }
      })
    },
    // 关闭弹窗
    handleClose () {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 重置表单
      this.formData = this.$options.data().formData
      this.$nextTick(() => {
        this.$refs.addDeptRef.resetFields()
      })
    }
  }
}
</script>

<style scoped lang='less'>
</style>
>>>>>>> 137875552f2efacb43668108598e0019e1ad86cf
